package io.sage.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import io.sage.common.utils.R;
import io.sage.config.IndustrySMS;
import io.sage.modules.employer.entity.Employer;
import io.sage.modules.employer.service.EmployerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 发送短信
 *
 */
@RestController
@RequestMapping("/sage/message")
@Api(tags="发送验证码接口")
public class MessageController {
	@Autowired
    private EmployerService studentService;
	/**
	 * 注册发送短信
	 * @param request
	 * @param jsonObject
	 * @return
	 */
	@PostMapping("sendSms")
    @ApiOperation("发送验证码")
    public R sendSms(HttpServletRequest request,@RequestBody JSONObject jsonObject){
		
		String phone = jsonObject.getString("phone");
		if(StringUtils.isEmpty(phone)){
    		return R.error("手机号不能为空");
    	}
		
		Employer student = studentService.selectByPhone(phone);
    	if(student!=null){
    		return R.error("手机号已经存在");
    	}
		
		//生成短信验证码
        String randomCode = RandomStringUtils.randomNumeric(4);
        
        System.out.println(randomCode);
        
        //第一个参数 发送给谁，第二个是随机数.
        String execute = IndustrySMS.execute(phone, randomCode);
        
        JSONObject jsonObject1 = JSONObject.parseObject(execute);
        String respCode = String.valueOf(jsonObject1.get("respCode"));
        
        if(!"00000".equals(respCode)){
        	return R.error("发送验证码失败");
        }else if ("00007".equals(respCode)) {
        	return R.error("验证码重复提交");
		}else if ("00008".equals(respCode)) {
        	return R.error("验证码操作频繁");
		}else if ("00019".equals(respCode)) {
        	return R.error("验证码订单已存在");
		}else if ("00025".equals(respCode)) {
        	return R.error("手机格式不对");
		}
        
        System.out.println(execute);
        
        //将短信验证码保存到session中
        request.getSession().setAttribute("randomCode",randomCode);
        
		return R.ok("系统已经将短信发送给到"+phone+"，请注意查收。");
        
    }
	/**
	 * 发送短信找回密码
	 * @param request
	 * @param jsonObject
	 * @return
	 */
	@PostMapping("sendSmsBack")
	@ApiOperation("找回发送验证码")
	public R sendSmsBack(HttpServletRequest request,@RequestBody JSONObject jsonObject){
		
		String phone = jsonObject.getString("phone");
		if(StringUtils.isEmpty(phone)){
			return R.error("手机号不能为空");
		}
		
		Employer student = studentService.selectByPhone(phone);
		if(student==null){
			return R.error("手机号没有注册");
		}
		
		//生成短信验证码
		String randomCode = RandomStringUtils.randomNumeric(4);
		
		System.out.println(randomCode);
		
		//第一个参数 发送给谁，第二个是随机数.
		String execute = IndustrySMS.execute(phone, randomCode);
		
		JSONObject jsonObject1 = JSONObject.parseObject(execute);
		String respCode = String.valueOf(jsonObject1.get("respCode"));
		
		if(!"00000".equals(respCode)){
			return R.error("发送验证码失败");
		}else if ("00007".equals(respCode)) {
			return R.error("验证码重复提交");
		}else if ("00008".equals(respCode)) {
			return R.error("验证码操作频繁");
		}else if ("00019".equals(respCode)) {
			return R.error("验证码订单已存在");
		}else if ("00025".equals(respCode)) {
			return R.error("手机格式不对");
		}else if ("00141".equals(respCode)) {
			return R.error("手机次数超时");
		}
		
		System.out.println(execute);
		
		//将短信验证码保存到session中
		request.getSession().setAttribute("randomCode",randomCode);
		
		return R.ok("系统已经将短信发送给到"+phone+"，请注意查收。");
		
	}
	
	public static void main(String[] args) {
		String randomCode = RandomStringUtils.randomNumeric(4);
		System.out.println(randomCode);
	}

}
